<!--就诊人管理-->
<template>
  <div>
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>就诊人管理</span>
          <el-button class="button" type="success" :icon="User" v-on:click="addUser" >添加就诊人</el-button>
        </div>
      </template>
      <!--就诊人管理信息-->
      <div v-if="scene==0" class="visitors">
        <Visitor class="visitorItem"
                 v-for="(user,index) in userArr"
                 :key="user.id"
                 :user="user"
                 :index="index"
                 @changeScene="changeScene"
                 @removeUser="removeUser"
        >

        </Visitor>
      </div>
      <!--添加就诊人/修改已有就诊人信息-->
      <div v-if="scene==1" class="changeVisitorFrom">
        <el-divider content-position="left">就诊人信息</el-divider>
        <el-form style="margin:10px auto;width: 60%;">
          <el-form-item label="用户姓名">
            <el-input placeholder="请输入姓名" v-model="addUserParams.name"></el-input>
          </el-form-item>
          <el-form-item label="证件类型">
            <el-select placeholder="请选择证件类型" style="width: 100%" v-model="addUserParams.certificatesType" >
              <el-option v-for="item in certificateArr" :key="item.id" :label="item.name" :value="item.value" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="证件号码">
            <el-input placeholder="请输入证件号码" v-model="addUserParams.certificatesNo" ></el-input>
          </el-form-item>
          <el-form-item label="用户性别">
            <el-radio-group v-model="addUserParams.sex" >
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="出生日期">
            <el-date-picker
                v-model="addUserParams.birthdate"
                value-format="YYYY-MM-DD"
                type="date"
                placeholder="请选择日期"
                :size="size"
            />
          </el-form-item>
          <el-form-item label="手机号码">
            <el-input placeholder="请输入手机号" v-model="addUserParams.phone" ></el-input>
          </el-form-item>
        </el-form>
        <el-divider content-position="left">建档信息</el-divider>
        <el-form style="margin:10px auto;width: 60%;">
          <el-form-item label="婚姻状况">
            <el-radio-group v-model="addUserParams.isMarry" >
              <el-radio :label="1">已婚</el-radio>
              <el-radio :label="0">未婚</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="支付方式">
            <el-radio-group v-model="addUserParams.isInsure" >
              <el-radio :label="1">医保</el-radio>
              <el-radio :label="0">自费</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="当前住址">
            <el-cascader :props="cityProps" v-model="addUserParams.addressSelected" ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址">
            <el-input placeholder="请填写详细地址" v-model="addUserParams.address" ></el-input>
          </el-form-item>
        </el-form>
        <el-divider content-position="left">紧急联系人</el-divider>
        <el-form style="margin:10px auto;width: 60%;" label-width="80" >
          <el-form-item label="用户姓名">
            <el-input placeholder="请输入姓名" v-model="addUserParams.contactsName" ></el-input>
          </el-form-item>
          <el-form-item label="证件类型">
            <el-select placeholder="请选择证件类型" v-model="addUserParams.contactsCertificatesType" style="width: 100%">
              <el-option v-for="item in certificateArr" :key="item.id" :label="item.name" :value="item.value" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="证件号码">
            <el-input placeholder="请输入证件号码" v-model="addUserParams.contactsCertificatesNo"></el-input>
          </el-form-item>
          <el-form-item label="手机号码">
            <el-input placeholder="请输入手机号" v-model="addUserParams.contactsPhone "></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="default" @click="Submit">提交</el-button>
            <el-button type="primary" size="default" @click="resetHandler" >重置</el-button>
          </el-form-item>
        </el-form>

      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">

import {User} from "@element-plus/icons-vue";
import Visitor from "@/components/visitor/visitor.vue";

import {reqGetUser} from "@/api/hospital";
import {onMounted, reactive, ref, watch} from "vue";
import {UserArr, UserResponseData} from "@/api/hospital/type.ts";
import {reqAddOrUpdateUser, reqCertificateType, reqCity} from "@/api/user";
import {AddOrUpdateUser, CertificateArr, CertificateTypeResponseData} from "@/api/user/type.ts";
import {CascaderProps, ElMessage} from "element-plus";
import {useRoute, useRouter} from "vue-router";

let $route = useRoute();
let $router = useRouter();

//存储全部就诊人的信息
let userArr = ref<UserArr>([]);

//控制展示就诊人管理信息还是修改删除信息
let scene = ref<number>(0);

//存储证件类型的数据
let certificateArr = ref<CertificateArr>([]);

//收集新增就诊人数据
let addUserParams = reactive<AddOrUpdateUser>({
  name: "",
  certificatesType: "",
  certificatesNo: "",
  sex: 1,
  birthdate: "",
  phone: "",
  isMarry: 0,
  isInsure: 0,
  addressSelected: [],
  address: "",
  contactsName: "",
  contactsCertificatesType: "",
  contactsCertificatesNo: "",
  contactsPhone: "",
});
//获取就诊人信息
const getAllVisitorUser = async () => {
  let result: UserResponseData = await reqGetUser();
  if (result.code == 200) {
    userArr.value = result.data;
  }
}

// 获取证件类型
const getCertificateType = async () => {
  let result: CertificateTypeResponseData = await reqCertificateType();
  if (result.code == 200) {
    certificateArr.value = result.data;
  }
}

//天就就诊人按钮回调
const addUser = () => {
  // 清空上一次的数据
  resetAddUserParams();
  //显示 添加就诊人信息的内容
  scene.value = 1;
}
//清空数据
const resetAddUserParams = () => {
  Object.assign(addUserParams,{
    id: null,
    name: "",
    certificatesType: "",
    certificatesNo: "",
    sex: 0,
    birthdate: "",
    phone: "",
    isMarry: 0,
    isInsure: 0,
    addressSelected: [],
    address: "",
    contactsName: "",
    contactsCertificatesType: "",
    contactsCertificatesNo: "",
    contactsPhone: "",
  });
}
//就诊人组件自定义事件
const changeScene = (user: AddOrUpdateUser) => {
  //收集已有的就诊人信息
  Object.assign(addUserParams,user)
  scene.value = 1;
};

//子组件自定义事件:删除按钮触发
const removeUser = () => {
  getAllVisitorUser();
}

//级联选择器
const cityProps: CascaderProps = {
  lazy: true,
  //加载数据的方法
  async lazyLoad(node: any, resolve: any) {
    let result: any = await reqCity(node.data.id || "86");
    //整理数据
    let showData = result.data.map(((item: any) => {
      return {
        id: item.id,
        label: item.name,
        value: item.value,
        leaf: !item.hasChildren,
      }
    }));
    //注入组件展示需要的数据
    resolve(showData);
  },
}

//提交按钮回调
const Submit = async () => {
  try {
    //新增就诊人/更新已有的用户
    await reqAddOrUpdateUser(addUserParams);
    ElMessage({
      type: "success",
      message: addUserParams.id ? "更新成功" : "新增成功",
      showClose: true,
    });
    //判断用户是不是从预约挂号而来
    if ($route.query.type) {
      //返回
      $router.back();
    } else {
      // 切换为展示就诊人场景
      scene.value = 0;
    }
    //重新获取全部就诊人信息
    await getAllVisitorUser();
  } catch (error){
    ElMessage({
      type: "error",
      message: addUserParams.id ? "更新失败" : "新增失败",
      showClose: true,
    });
  }
}
//重置按钮回调
const resetHandler = () => {
  resetAddUserParams();
}

onMounted(() => {
  //获取就诊人信息
  getAllVisitorUser();
  //获取证件类型信息
  getCertificateType();
  //判断当前路由组件是不是用户从挂号组件过来的(挂号组件过来的会携带type=add的query参数
  if ($route.query.type == "add") {
    //切换到添加就诊人场景
    scene.value = 1;
  }
  if ($route.query.type == 'edit') {
    scene.value = 1;
  }
});

//监听所有就诊人的数据变化
watch(
    ()=>userArr.value,
    () => {
      if ($route.query.type == 'edit') {
        let user = userArr.value.find((item: any) => {
          return item.id == $route.query.id;
        });
        Object.assign(addUserParams, user);
      }
    }
);

</script>

<style scoped lang="scss">
:deep(.box-card) {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .el-card__body {
    .visitors {
      display: flex;
      //换行
      flex-wrap: wrap;
      .visitorItem {
        width: 32%;
        margin: 5px;
      }
    }
  }
}
</style>